package com.baiyinet.biz;

import android.content.SharedPreferences;
import android.text.TextUtils;

import com.baiyinet.Constants;
import com.baiyinet.rest.AppInfo;
import com.baiyinet.rest.Link;
import com.baiyinet.util.CookieHelper;

import java.util.List;

/**
 * @author J.Huang
 * @version 1.0
 * @since 16/12/11
 */

public class CurrentUser {

    private final SharedPreferences mSharedPref;

    private AppInfo mAppInfo = null;
    private String mLink = "";
    private String mChannelInfo = "";
    private List<Link> mAddLinkList;
    private String mAttachData = "";

    private String mBackActionTarget = "";
    private boolean mBackActionReload = false;
    private int mCurrentTabIndex = 0;

    private long mAppLastUsedTime = 0;

    public CurrentUser(SharedPreferences sharedPref) {
        mSharedPref = sharedPref;
    }

    public void setAppInfo(AppInfo appInfo) {
        mAppInfo = appInfo;
    }

    public boolean hasLoggedIn() {
        return (mAppInfo != null
                && ("unneed".equals(mAppInfo.NeedLogin.toLowerCase())
                    || !TextUtils.isEmpty(mSharedPref.getString(Constants.KEY_COOKIE, ""))
                    )
                );
    }

    public int getLatestAppVersion() {
        return mAppInfo != null ? mAppInfo.AppVersion : 0;
    }

    public String getAppDownloadUrl() {
        return mAppInfo != null ? mAppInfo.AppUrl : "";
    }

    public boolean needLogin() {
        return (mAppInfo != null) && "need".equals(mAppInfo.NeedLogin.toLowerCase());
    }

    public boolean hasMultiLoggedIn() {
        return (mAppInfo != null) && "multilogin".equals(mAppInfo.UserStatus.toLowerCase());
    }

    public String getToolbarBgColor() {
        return mAppInfo != null ? mAppInfo.TitlebarBg : "";
    }

    public void logout() {
        setAppInfo(null);
        setLink("");
        setChannelInfo("");

        // 删除保存的用户信息
        SharedPreferences.Editor editor = mSharedPref.edit();
        editor.putString(Constants.KEY_COOKIE, "");
        editor.apply();

        CookieHelper.RemoveCookie(null);
    }

    public String getCurrentCookie() {
        String strCookie = mSharedPref.getString(Constants.KEY_COOKIE, "");
        String cookies[] = strCookie.split(";");
        if (mAppInfo != null) {
            for (String cookie : cookies) {
                if (cookie.trim().startsWith(mAppInfo.CookieName)) {
                    SharedPreferences.Editor editor = mSharedPref.edit();
                    editor.putString(Constants.KEY_COOKIE, cookie);
                    editor.apply();

                    return cookie;
                }
            }
        }
        return "";
    }

    public String getCompleteCookie() {

        return mSharedPref.getString(Constants.KEY_COOKIE, "");
    }

    public void setLink(String link) {
        mLink = link;
    }

    public String getLink() {
        return mLink;
    }

    public String getChannelInfo() {
        return mChannelInfo;
    }

    public void setChannelInfo(String channelInfo) {
        mChannelInfo = channelInfo;
    }

    public List<Link> getAddLinkList() {
        return mAddLinkList;
    }

    public void setAddLinkList(List<Link> addLinkList) {
        this.mAddLinkList = addLinkList;
    }

    public void setImageUploadAttachData(String attachData) {
        mAttachData = attachData;
    }

    public String getAttachData() {
        return mAttachData;
    }

    public String getBackActionTarget() {
        return mBackActionTarget;
    }

    public boolean getBackActionReload() {
        return mBackActionReload;
    }

    public void setBackAction(String backActionTarget, boolean reload) {
        this.mBackActionTarget = backActionTarget;
        this.mBackActionReload = reload;
    }

    public boolean isAppNameShown() {
        return mAppInfo != null && "hide".equalsIgnoreCase(mAppInfo.ShowAppName);
    }

    public void setCurrentTab(int tabIndex) {
        mCurrentTabIndex = tabIndex;
    }

    public int getCurrentTab() {
        return mCurrentTabIndex;
    }

    public long getAppLastUsedTime() {
        return mAppLastUsedTime;
    }

    public void setAppLastUsedTime(long appLastUsedTime) {
        mAppLastUsedTime = appLastUsedTime;
    }
}
